<?php
/**
 * Created by PhpStorm.
 * User: tengjs
 * Date: 2020/11/3
 * Time: 7:37
 */

namespace App\Http\Controllers\Api\Index;

use App\Http\Controllers\Api\BaseController;
use App\Admin\Models\ContentModel;

class IndexController extends BaseController
{
    /**
     * 友情链接列表
     *
     * @return array
     */
    public function friendlinkList()
    {
        $lang = $this->input('lang');
        $file = ['content_id', 'title_zh as title', 'content_url'];
        if ($lang == 'en') {
            $file = ['content_id', 'title_en as title', 'image_url as content_url'];
        }
        $list = ContentModel::select($file)->where('content_type', '=', 8)->orderBy('order', 'asc')->orderBy('content_id', 'desc')->get();

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => [
                'list' => $list
            ]
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }

    /**
     * 轮播图列表
     *
     * @return array
     */
    public function bannerList()
    {
        $lang = $this->input('lang');
        $file = ['content_id', 'image_url', 'title_zh as title', 'content_url'];
        if ($lang == 'en') {
            $file = ['content_id', 'image_url', 'title_en as title', 'content_url'];
        }
        $list = ContentModel::select($file)->where('content_type', '=', 9)->orderBy('order', 'asc')->orderBy('content_id', 'desc')->get();
        foreach ($list as &$item) {
            $item['image_url'] = $this->buildImageUrl($item['image_url']);
        }
        unset($item);

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => [
                'list' => $list
            ]
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }

    /**
     * 首页数据
     *
     * @return array
     */
    public function homeInfo()
    {
        $contentId = 3;
        $file = [
            'content_id', 'title_zh as titleZh', 'title_en as titleEn', 'content_date as titleAbbreviation', 'phone as copyright', 'content_url as logo_url',
            'image_url as banner_url', 'content_en as introduction'
        ];
        $selectResult = ContentModel::select($file)->where('content_id', '=', $contentId)->first();
        $data = [];
        if (!empty($selectResult)) {
            $data = $selectResult->toArray();
            if (!empty($data['logo_url'])) {
                $data['logo_url'] = $this->buildImageUrl($data['logo_url']);
            }
            if (!empty($data['banner_url'])) {
                $data['banner_url'] = $this->buildImageUrl($data['banner_url']);
            }
        }

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => $data
        ];
        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }

    /**
     * footer
     *
     * @return array
     */
    public function footer()
    {
        // Contact Info
        $contentId = 2;
        $file = [
            'content_id', 'content_url as email', 'phone', 'content_date as postal_code', 'title_en as address',
            'introduction_en as subway_mode', 'content_en as bus_mode', 'image_url as address_image_url'
        ];
        $selectResult = ContentModel::select($file)->where('content_id', '=', $contentId)->first();
        $contactInfo = [];
        if (!empty($selectResult)) {
            $contactInfo = $selectResult->toArray();
            if (!empty($contactInfo['address_image_url'])) {
                $contactInfo['address_image_url'] = $this->buildImageUrl($contactInfo['address_image_url']);
            }
        }

        // useful links
        $file = ['content_id', 'title_en as title', 'image_url as content_url'];
        $linkList = ContentModel::select($file)->where('content_type', '=', 8)
            ->orderBy('order', 'asc')
            ->orderBy('content_id', 'desc')
            ->limit(3)
            ->get();

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => [
                'contact_info' => $contactInfo,
                'useful_link_list' => $linkList
            ]
        ];
        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }

    /**
     * 近期论文列表
     *
     * @return array
     */
    public function recentPublicationsList()
    {
        $lang = $this->input('lang');
        if ($lang == 'en') {
            $recentFile = ['content_id', 'title_en as title', 'image_url as content_url'];
        } else {
            $recentFile = ['content_id', 'title_zh as title', 'content_url'];
        }

        // 近期论文
        $recentList = ContentModel::select($recentFile)
            ->where('content_type', '=', 13)
            ->where('title_' . $lang, '<>', '')
            ->orderBy('order', 'asc')
            ->orderBy('content_id', 'desc')
            ->get();

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => [
                'recent_list' => $recentList
            ]
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }
}
